﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Security.Cryptography;


namespace Talk
{
    public partial class unfollow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null) Response.Redirect("/index.aspx");
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            errormsg.Text = "";
            if (Session["UserID"] == null)
            {
                Response.Redirect("/index.aspx");
                return;
            }
            Regex r = new Regex("[^a-zA-Z0-9]");
            if (username.Text.Length < 3 || username.Text.Length > 12 || r.IsMatch(username.Text))
            {
                errormsg.Text = "用户名非法！";
                return;
            }
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["talkconn"].ConnectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select UserID from Users"
                    + " where Username='" + username.Text + "'";
                SqlDataReader data = cmd.ExecuteReader();
                if (!data.HasRows)
                {
                    errormsg.Text = "用户不存在！";
                    data.Close();
                    conn.Close();
                    return;
                }
                data.Read();
                string following = data[0].ToString();
                data.Close();
                cmd.CommandText = "delete from Follows where Follower='"
                    + Session["UserID"].ToString() + "' and Following='" + following + "'";
                if(cmd.ExecuteNonQuery()>0)
                {
                    cmd.CommandText = "update Users set nFollowing = nFollowing - 1"
                        + " where UserID = '" + Session["UserID"].ToString() + "'";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "update Users set nFollower = nFollower - 1"
                        + " where UserID = '" + following + "'";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "select nFollowing from Users where UserID = '"
                        + Session["UserID"].ToString() + "'";
                    data = cmd.ExecuteReader();
                    if(data.HasRows)
                    {
                        data.Read();
                        Session["nFollowing"] = data[0];
                        data.Close();
                    }
                }
                cmd.CommandText = "insert into Actions(UserID,What,Time,IP,Target) values("
      + Session["UserID"].ToString()
      + ",'" + Session["Username"] + "取消跟随" + username.Text + "','"
      + DateTime.Now.ToString() + "','"
      + Request.UserHostAddress.ToString()
      + "'," + following + ");";
                cmd.ExecuteNonQuery();

                conn.Close();
                Response.Redirect("/index.aspx");
            }
            catch (System.Exception ex)
            {
                errormsg.Text = ex.Message;
            }
        }
    }
}
